import hsr.BSP;

import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PVector;
import processing.event.Event;
import remixlab.proscene.CameraConstraint;
import remixlab.proscene.CameraProfile;
import remixlab.proscene.Quaternion;
import remixlab.proscene.Scene;
import shape.Box;
import shape.Polyhedra;
import shape.PolyhedraFace;
import shape.maze.Maze;
import shape.maze.MazeGenerator;
import camera.MazeCamera;

public class MyProcessingSketch extends PApplet {

	Scene scene;
	BSP hsr;
	ArrayList<Polyhedra> polyhedrons;
	private MazeCamera wProfile;
	private final int MAX_HEIGHT = 30;
	
	public void setup() {
		 size(640, 360, P3D);
		 hint(DISABLE_DEPTH_TEST);
		// Scene instantiation
		scene = new Scene(this);
		polyhedrons = new ArrayList<Polyhedra>();
		//Set the camera to First Person
		/*wProfile = new MazeCamera(scene, "MY_PROFILE");
	    scene.registerCameraProfile(wProfile);
		scene.unregisterCameraProfile("ARCBALL");
		scene.unregisterCameraProfile("WHEELED_ARCBALL");
		scene.unregisterCameraProfile("FIRST_PERSON");
		
		CameraProfile FP = scene.currentCameraProfile();
		FP.setCameraMouseBinding(Event.SHIFT, LEFT, Scene.MouseAction.ROTATE);   
		scene.camera().setFlySpeed(1);
		scene.camera().frame().setPosition(new PVector(100, 100, 20));
		scene.camera().frame().rotate(new Quaternion(new PVector(0,0,1), radians(-45)));
		scene.camera().frame().rotate(new Quaternion(new PVector(1,0,0), radians(-90)));
		
		scene.camera().frame().setConstraint(new CameraConstraint(scene.camera()));*/
		
		//drawStadium();
		//drawTable();
		
		MazeGenerator mazeGen = new MazeGenerator(10,10);
		Maze maze = mazeGen.getRandomMaze();
		System.out.println(maze.getStringRepresentation(2));
		polyhedrons.addAll(maze.getPolyhedraRepresentation(10, 0.0f));
		hsr = new BSP(polyhedrons);
	}



	



	public void draw() {
		background(255);
		
//		for(Polyhedra p : polyhedrons){
//			p.draw(this);
//		}
		
		hsr.draw(this, scene.camera().position());
		
		//Add the constraints for the floor
		//mantainOverFloor();
		//manteinUnderRoof();
				
	}
	
	private void manteinUnderRoof() {
		if(scene.camera().position().z > MAX_HEIGHT) {
			PVector limit = scene.camera().position();
			scene.camera().setPosition(new PVector(limit.x, limit.y, MAX_HEIGHT-5));
		}
	}



	private void mantainOverFloor() {
		if(scene.camera().position().z < 0) {
			PVector limit = scene.camera().position();
			scene.camera().setPosition(new PVector(limit.x, limit.y, 5));
		}
		
	}



	private void drawStadium() {
		
		//Draw main columns
		
		Box c1 = new Box(60, 80, 60, 80, 50, 0,Box.DEFAULT_COLORS);
		polyhedrons.add(c1);
		Box c2 = new Box(-80, -60, 60, 80, 50, 0,Box.DEFAULT_COLORS);
		polyhedrons.add(c2);
		Box c3 = new Box(-80, -60, -80, -60, 50, 0,Box.DEFAULT_COLORS);
		polyhedrons.add(c3);
		Box c4 = new Box(60, 80, -80, -60, 50, 0,Box.DEFAULT_COLORS);
		polyhedrons.add(c4);
		
		
		//Draw ceiling
		
		PolyhedraFace t1 = new PolyhedraFace(5);
		t1.addVertex(80,60, 51);
		t1.addVertex(40,60, 51);
		t1.addVertex(40,-60, 51);
		t1.addVertex(80,-60, 51);
		Polyhedra p1 = new Polyhedra();
		p1.addFace(t1);
		polyhedrons.add(p1);
		
		
		PolyhedraFace t2 = new PolyhedraFace(5);
		t2.addVertex(60,40, 51);
		t2.addVertex(60,80, 51);
		t2.addVertex(-60,80, 51);
		t2.addVertex(-60,40, 51);
		Polyhedra p2 = new Polyhedra();
		p2.addFace(t2);
		polyhedrons.add(p2);
		
		
		PolyhedraFace t3 = new PolyhedraFace(5);
		t3.addVertex(-40,60, 51);
		t3.addVertex(-80,60, 51);
		t3.addVertex(-80,-60, 51);
		t3.addVertex(-40,-60, 51);
		Polyhedra p3 = new Polyhedra();
		p3.addFace(t3);
		polyhedrons.add(p3);
		
		
		PolyhedraFace t4 = new PolyhedraFace(5);
		t4.addVertex(-60,-40, 51);
		t4.addVertex(-60,-80, 51);
		t4.addVertex(60,-80, 51);
		t4.addVertex(60,-40, 51);
		Polyhedra p4 = new Polyhedra();
		p4.addFace(t4);
		polyhedrons.add(p4);
		
		
		//Draw rostrum
		
		PolyhedraFace g1 = new PolyhedraFace(100);
		g1.addVertex(80,60, 50);
		g1.addVertex(40,60, 0);
		g1.addVertex(40,-60, 0);
		g1.addVertex(80,-60, 50);
		Polyhedra  r1= new Polyhedra();
		r1.addFace(g1);
		polyhedrons.add(r1);
		
		PolyhedraFace g2 = new PolyhedraFace(100);
		g2.addVertex(60,40, 0);
		g2.addVertex(60,80, 50);
		g2.addVertex(-60,80, 50);
		g2.addVertex(-60,40, 0);
		Polyhedra  r2= new Polyhedra();
		r2.addFace(g2);
		//polyhedrons.add(r2);
		
		PolyhedraFace g3 = new PolyhedraFace(100);
		g3.addVertex(-40,60, 0);
		g3.addVertex(-80,60, 50);
		g3.addVertex(-80,-60, 50);
		g3.addVertex(-40,-60, 0);
		Polyhedra  r3= new Polyhedra();
		r3.addFace(g3);
		polyhedrons.add(r3);
		
		
		//Draw field
		
		PolyhedraFace f = new PolyhedraFace(color(52, 179, 37));
		f.addVertex(30,30, 0.1f);
		f.addVertex(-30,30, 0.1f);
		f.addVertex(-30,-30, 0.1f);
		f.addVertex(30,-30, 0.1f);
		Polyhedra  c= new Polyhedra();
		c.addFace(f);
		polyhedrons.add(c);
		
		
		//Draw the floor
		PolyhedraFace floor = new PolyhedraFace(200);
		floor.addVertex(-100, -100, -0.1f);
		floor.addVertex(100, -100, -0.1f);
		floor.addVertex(100, 100, -0.1f);
		floor.addVertex(-100, 100, -0.1f);
		Polyhedra p = new Polyhedra();
		p.addFace(floor);
		polyhedrons.add(p);
	}
	
	
private void drawTable() {
		
		
		//Draw Table
		Box c1 = new Box(-60,60,-40,40,35,30, 0xFF0099FF);
		polyhedrons.add(c1);
		
		PolyhedraFace l = new PolyhedraFace(0);
		l.addVertex(40, -60, 29.5f);
		l.addVertex(40, 60, 29.5f);
		l.addVertex(-40, 60, 29.5f);
		l.addVertex(-40, -60, 29.5f);
		Polyhedra t1 = new Polyhedra();
		t1.addFace(l);
		polyhedrons.add(t1);
		
		
		
		//Draw lines
		l = new PolyhedraFace(255);
		l.addVertex(40, 60, 35.5f);
		l.addVertex(38, 60, 35.5f);
		l.addVertex(38, -60, 35.5f);
		l.addVertex(40, -60, 35.5f);
		Polyhedra l1 = new Polyhedra();
		l1.addFace(l);
		polyhedrons.add(l1);
		
		
		l = new PolyhedraFace(255);
		l.addVertex(40, 60, 35.5f);
		l.addVertex(40, 58, 35.5f);
		l.addVertex(-40, 58, 35.5f);
		l.addVertex(-40, 60, 35.5f);
		Polyhedra l2 = new Polyhedra();
		l2.addFace(l);
		polyhedrons.add(l2);
		
		
		l = new PolyhedraFace(255);
		l.addVertex(-40, 60, 35.5f);
		l.addVertex(-40, -60, 35.5f);
		l.addVertex(-38, -60, 35.5f);
		l.addVertex(-38, 60, 35.5f);
		Polyhedra l3 = new Polyhedra();
		l3.addFace(l);
		polyhedrons.add(l3);
		
		l = new PolyhedraFace(255);
		l.addVertex(-40, -60, 35.5f);
		l.addVertex(40, -60, 35.5f);
		l.addVertex(40, -58, 35.5f);
		l.addVertex(-40, -58, 35.5f);
		Polyhedra l4 = new Polyhedra();
		l4.addFace(l);
		polyhedrons.add(l4);
		
		l = new PolyhedraFace(255);
		l.addVertex(1, 60, 35.5f);
		l.addVertex(-1, 60, 35.5f);
		l.addVertex(-1, -60, 35.5f);
		l.addVertex(1, -60, 35.5f);
		Polyhedra l5 = new Polyhedra();
		l5.addFace(l);
		polyhedrons.add(l5);
		
		//Draw net
		l = new PolyhedraFace(200);
		l.addVertex(40, 0, 35.5f);
		l.addVertex(40, 0, 43);
		l.addVertex(-40, 0, 43);
		l.addVertex(-40, 0, 35.5f);
		Polyhedra net = new Polyhedra();
		net.addFace(l);
		polyhedrons.add(net);
		
		//Draw "patas"
		Box p1 = new Box(30,40,20,30,29.5f,0, 0xFFAACCFF);
		polyhedrons.add(p1);
		Box p2 = new Box(30,40,-30,-20,29.5f,0, 0xFFCCAAFF);
		polyhedrons.add(p2);
		Box p3 = new Box(-40,-30,20,30,29.5f,0, 0xFFCCFFAA);
		polyhedrons.add(p3);
		Box p4 = new Box(-40,-30,-30,-20,29.5f,0, 0xAAFCF796);
		polyhedrons.add(p4);
		
	
		//Draw Floor
		PolyhedraFace floor = new PolyhedraFace(100);
		floor.addVertex(-100, -100, -0.1f);
		floor.addVertex(100, -100, -0.1f);
		floor.addVertex(100, 100, -0.1f);
		floor.addVertex(-100, 100, -0.1f);
		Polyhedra p = new Polyhedra();
		p.addFace(floor);
		polyhedrons.add(p);
		
	}
}
